﻿using System;
using System.Reflection;

namespace XamlMvp
{
    internal class AutoEventBinding
    {
        readonly EventInfo eventInfo;
        readonly MethodInfo methodInfo;

        public AutoEventBinding(EventInfo eventInfo, MethodInfo methodInfo)
        {
            this.eventInfo = eventInfo;
            this.methodInfo = methodInfo;
        }

        Delegate CreateDelegate(object eventHandler)
        {
            return Delegate.CreateDelegate(eventInfo.EventHandlerType, eventHandler, methodInfo);
        }

        public void BindEvent(object eventSource, object eventHandler)
        {
            eventInfo.AddEventHandler(eventSource, CreateDelegate(eventHandler));
        }

        public void UnBindEvent(object eventSource, object eventHander)
        {
            eventInfo.RemoveEventHandler(eventSource, CreateDelegate(eventHander));
        }
    }
}
